﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;

using HsTec.Mobile.Green.Util;

namespace HsTec.Mobile.Green.DAL
{
    /// <summary>
    /// 数据访问类Commitment。
    /// </summary>
    public class CommitmentDAL
    {
        public CommitmentDAL()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string sExistsWhere)
        {
            return SQLHelper.Exists("pm_tp_Commitment", sExistsWhere);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(HsTec.Mobile.Green.Model.Commitment model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into pm_tp_Commitment(");
            strSql.Append("professional,departments,completiondate,deliverydate,reviewer,branchreviewer,qualityplan,conferencereview,tp_id)");
            strSql.Append(" values (");
            strSql.Append("@professional,@departments,@completiondate,@deliverydate,@reviewer,@branchreviewer,@qualityplan,@conferencereview,@tp_id)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@professional", SqlDbType.Char,1),
					new SqlParameter("@departments", SqlDbType.Int,4),
					new SqlParameter("@completiondate", SqlDbType.DateTime),
					new SqlParameter("@deliverydate", SqlDbType.DateTime),
					new SqlParameter("@reviewer", SqlDbType.Int,4),
					new SqlParameter("@branchreviewer", SqlDbType.Int,4),
					new SqlParameter("@qualityplan", SqlDbType.Char,1),
					new SqlParameter("@conferencereview", SqlDbType.Char,1),
					new SqlParameter("@tp_id", SqlDbType.Int,4)};
            parameters[0].Value = model.professional;
            parameters[1].Value = model.departments;
            parameters[2].Value = model.completiondate;
            parameters[3].Value = model.deliverydate;
            parameters[4].Value = model.reviewer;
            parameters[5].Value = model.branchreviewer;
            parameters[6].Value = model.qualityplan;
            parameters[7].Value = model.conferencereview;
            parameters[8].Value = model.tp_id;

            return SQLHelper.ExecuteNonQuery(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(HsTec.Mobile.Green.Model.Commitment model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update pm_tp_Commitment set ");
            strSql.Append("professional=@professional,");
            strSql.Append("departments=@departments,");
            strSql.Append("completiondate=@completiondate,");
            strSql.Append("deliverydate=@deliverydate,");
            strSql.Append("reviewer=@reviewer,");
            strSql.Append("branchreviewer=@branchreviewer,");
            strSql.Append("qualityplan=@qualityplan,");
            strSql.Append("conferencereview=@conferencereview,");
            strSql.Append("tp_id=@tp_id");
            strSql.Append(" where com_id=@com_id ");
            SqlParameter[] parameters = {
					new SqlParameter("@com_id", SqlDbType.Int,4),
					new SqlParameter("@professional", SqlDbType.Char,1),
					new SqlParameter("@departments", SqlDbType.Int,4),
					new SqlParameter("@completiondate", SqlDbType.DateTime),
					new SqlParameter("@deliverydate", SqlDbType.DateTime),
					new SqlParameter("@reviewer", SqlDbType.Int,4),
					new SqlParameter("@branchreviewer", SqlDbType.Int,4),
					new SqlParameter("@qualityplan", SqlDbType.Char,1),
					new SqlParameter("@conferencereview", SqlDbType.Char,1),
					new SqlParameter("@tp_id", SqlDbType.Int,4)};
            parameters[0].Value = model.com_id;
            parameters[1].Value = model.professional;
            parameters[2].Value = model.departments;
            parameters[3].Value = model.completiondate;
            parameters[4].Value = model.deliverydate;
            parameters[5].Value = model.reviewer;
            parameters[6].Value = model.branchreviewer;
            parameters[7].Value = model.qualityplan;
            parameters[8].Value = model.conferencereview;
            parameters[9].Value = model.tp_id;

            return SQLHelper.ExecuteNonQuery(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public int Delete(string sIDs)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" where com_id in ( " + sIDs + " )");
            return SQLHelper.ExecuteNonQuery(strSql.ToString());
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public HsTec.Mobile.Green.Model.Commitment GetModel(int com_id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 com_id,professional,departments,completiondate,deliverydate,reviewer,branchreviewer,qualityplan,conferencereview,tp_id from pm_tp_Commitment ");
            strSql.Append(" where com_id=@com_id ");
            SqlParameter[] parameters = {
					new SqlParameter("@com_id", SqlDbType.Int,4)};
            parameters[0].Value = com_id;

            HsTec.Mobile.Green.Model.Commitment model = new HsTec.Mobile.Green.Model.Commitment();
            DataSet ds = SQLHelper.ExecuteDataset(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["com_id"].ToString() != "")
                {
                    model.com_id = int.Parse(ds.Tables[0].Rows[0]["com_id"].ToString());
                }
                model.professional = ds.Tables[0].Rows[0]["professional"].ToString();
                if (ds.Tables[0].Rows[0]["departments"].ToString() != "")
                {
                    model.departments = int.Parse(ds.Tables[0].Rows[0]["departments"].ToString());
                }
                if (ds.Tables[0].Rows[0]["completiondate"].ToString() != "")
                {
                    model.completiondate = DateTime.Parse(ds.Tables[0].Rows[0]["completiondate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["deliverydate"].ToString() != "")
                {
                    model.deliverydate = DateTime.Parse(ds.Tables[0].Rows[0]["deliverydate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["reviewer"].ToString() != "")
                {
                    model.reviewer = int.Parse(ds.Tables[0].Rows[0]["reviewer"].ToString());
                }
                if (ds.Tables[0].Rows[0]["branchreviewer"].ToString() != "")
                {
                    model.branchreviewer = int.Parse(ds.Tables[0].Rows[0]["branchreviewer"].ToString());
                }
                model.qualityplan = ds.Tables[0].Rows[0]["qualityplan"].ToString();
                model.conferencereview = ds.Tables[0].Rows[0]["conferencereview"].ToString();
                if (ds.Tables[0].Rows[0]["tp_id"].ToString() != "")
                {
                    model.tp_id = int.Parse(ds.Tables[0].Rows[0]["tp_id"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataView GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select com_id,professional,departments,completiondate,deliverydate,reviewer,branchreviewer,qualityplan,conferencereview,tp_id ");
            strSql.Append(" FROM pm_tp_Commitment ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SQLHelper.ExecuteDataView(strSql.ToString());
        }

        public DataView GetListEntity(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select com_id,professional,departments,completiondate,deliverydate,reviewer,branchreviewer,qualityplan,conferencereview,tp_id,reviewername,branchreviewername,deptname ");
            strSql.Append(" FROM v_pm_tp_Commitment ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SQLHelper.ExecuteDataView(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataView GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" com_id,professional,departments,completiondate,deliverydate,reviewer,branchreviewer,qualityplan,conferencereview,tp_id ");
            strSql.Append(" FROM pm_tp_Commitment ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return SQLHelper.ExecuteDataView(strSql.ToString());
        }

        #endregion  成员方法
    }
}

